#include "citydao.h"

// 构造函数，初始化数据库连接参数
CityDao::CityDao(const QString& host, const QString& database, const QString& username, const QString& password)
    : host(host), database(database), username(username), password(password) {
}

// 析构函数，关闭数据库连接
CityDao::~CityDao() {
    closeDatabase();
}

// 打开数据库连接
bool CityDao::openDatabase() {
    if(db.isOpen()){
        return true;
    }
    // 数据库驱动为QMYSQL
    db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName(host);
    db.setDatabaseName(database);
    db.setUserName(username);
    db.setPassword(password);
    
    // 打开数据库
    if (!db.open()) {
        qDebug() << "Database connection failed:" << db.lastError().text();
        return false;
    }
    return true;
}

// 关闭数据库连接
void CityDao::closeDatabase() {
    if (db.isOpen()) {
        db.close();
    }
}

// 检查数据库连接状态
bool CityDao::isDatabaseOpen() const {
    return db.isOpen();
}

// 插入城市数据
bool CityDao::insertCity(const City& city) {
    if (!isDatabaseOpen()) {
        qDebug() << "Database is not open!";
        return false;
    }
    
    // 准备SQL语句
    QSqlQuery query;
    QString sql = "INSERT INTO city (city_id, city_name) VALUES (:cityId, :cityName)";
    query.prepare(sql);
    query.bindValue(":cityId", city.cityId);
    query.bindValue(":cityName", city.cityName);
    
    // 执行SQL语句
    if (!query.exec()) {
        qDebug() << "Insert failed:" << query.lastError().text();
        return false;
    }
    return true;
}

// 删除城市数据
bool CityDao::deleteCity(const QString& cityId) {
    if (!isDatabaseOpen()) {
        qDebug() << "Database is not open!";
        return false;
    }
    
    // 准备SQL语句
    QSqlQuery query;
    QString sql = "DELETE FROM city WHERE city_id = :cityId";
    query.prepare(sql);
    query.bindValue(":cityId", cityId);
    
    // 执行SQL语句
    if (!query.exec()) {
        qDebug() << "Delete failed:" << query.lastError().text();
        return false;
    }
    return query.numRowsAffected() > 0;
}

// 更新城市数据
bool CityDao::updateCity(const City& city) {
    if (!isDatabaseOpen()) {
        qDebug() << "Database is not open!";
        return false;
    }
    
    // 准备SQL语句
    QSqlQuery query;
    QString sql = "UPDATE city SET city_name = :cityName WHERE city_id = :cityId";
    query.prepare(sql);
    query.bindValue(":cityId", city.cityId);
    query.bindValue(":cityName", city.cityName);
    
    // 执行SQL语句
    if (!query.exec()) {
        qDebug() << "Update failed:" << query.lastError().text();
        return false;
    }
    return query.numRowsAffected() > 0;
}

// 查询单个城市数据
City* CityDao::getCity(const QString& cityId) {
    if (!isDatabaseOpen()) {
        qDebug() << "Database is not open!";
        return nullptr;
    }
    
    // 准备SQL语句
    QSqlQuery query;
    QString sql = "SELECT * FROM city WHERE city_id = :cityId";
    query.prepare(sql);
    query.bindValue(":cityId", cityId);
    
    // 执行SQL语句
    if (!query.exec()) {
        qDebug() << "Query failed:" << query.lastError().text();
        return nullptr;
    }
    
    // 解析查询结果
    if (query.next()) {
        City* city = new City();
        city->cityId = query.value("city_id").toString();
        city->cityName = query.value("city_name").toString();
        return city;
    }
    return nullptr;
}

// 查询所有城市数据
QList<City>* CityDao::getAllCities() {
    if (!isDatabaseOpen()) {
        qDebug() << "Database is not open!";
        return nullptr;
    }
    
    // 准备SQL语句
    QSqlQuery query;
    QString sql = "SELECT * FROM city";
    if (!query.exec(sql)) {
        qDebug() << "Query failed:" << query.lastError().text();
        return nullptr;
    }
    
    // 解析查询结果
    QList<City>* cities = new QList<City>();
    while (query.next()) {
        City city;
        city.cityId = query.value("city_id").toString();
        city.cityName = query.value("city_name").toString();
        cities->append(city);
    }
    return cities;
}

// 新增：通过城市名模糊查询城市ID
QList<City>* CityDao::searchCitiesByName(const QString& cityName) {
    if (!isDatabaseOpen()) {
        qDebug() << "Database is not open!";
        return nullptr;
    }
    
    // 准备SQL语句，使用LIKE进行模糊匹配
    QSqlQuery query;
    QString sql = "SELECT * FROM city WHERE city_name LIKE :cityName";
    query.prepare(sql);
    // 在查询字符串前后添加%，实现模糊匹配
    query.bindValue(":cityName", QString("%%1%").arg(cityName));
    
    if (!query.exec()) {
        qDebug() << "Search failed:" << query.lastError().text();
        return nullptr;
    }
    
    // 解析查询结果
    QList<City>* cities = new QList<City>();
    while (query.next()) {
        City city;
        city.cityId = query.value("city_id").toString();
        city.cityName = query.value("city_name").toString();
        cities->append(city);
    }
    
    return cities;
}
// 通过城市名查询城市ID
City* CityDao::getCityIDByName(const QString& cityName) {
    // 准备SQL语句，使用LIKE进行模糊匹配
    QSqlQuery query;
    QString sql = "SELECT * FROM city WHERE city_name = :cityName";
    query.prepare(sql);
    // 在查询字符串前后添加%，实现模糊匹配
    query.bindValue(":cityName", QString("%1").arg(cityName));
    
    if (!query.exec()) {
        qDebug() << "Search failed:" << query.lastError().text();
        return nullptr;
    }
    
    // 解析查询结果
    if (query.next()) {
        City* city = new City();
        city->cityId = query.value("city_id").toString();
        city->cityName = query.value("city_name").toString();
        return city;
    }
    
    return nullptr;
}
